/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {

    //1:首页广告, 3:专题
    var type = getUrlPath(3);

    //嵌入方id
    var partner_id = getUrlPath(4);

    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'embed_banners/' + partner_id,
                condition: {type: type},
                model: {
                    url: {
                        put: _global.url.api + 'embed_banner/{id}',
                        delete: _global.url.api + 'embed_banner/{id}'
                    },
                    event: {
                        '.item-online:click': 'online',
                        '.item-offline:click': 'offline',
                        '[name="listorder"]:change': 'changeListOrder',
                        '.item-remove': 'remove'
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function() {
                                _this.del();
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    online:function(e){
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function() {
                                _this.set_data('is_visible', 1);
                                _this.put()
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    changeListOrder:function(e){
                        e.preventDefault();
                        this.set_data('listorder', $(e.target).val());
                        this.put();
                    },
                    offline:function(e){
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function() {
                                _this.set_data('is_visible', 2);
                                _this.put()
                            }, cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function(e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title:'添加',
                    content: template('add-tpl', {}),
                    width: 600,
                    height: 400,
                    ok: function() {

                    },cancel: function() {}
                });
                _this.dialog.show();
                $('input,select,textarea').on('change', function(e) {
                    _this.change(e);
                });
                _this.getBanners();
            });

            //添加
            $('body').on('click', '.item-add', function(e) {
                e.preventDefault();
                var banner_id = $(this).attr('data-id');
                if((new Model({
                    url: _global.url.api + 'embed_banner',
                    data: {partner_id: partner_id, banner_id: banner_id}
                })).post()) {
                    _this.collection.reload();
                }
            });
        },

        getBanners: function() {
            /*var _this = this;
            if(typeof this.banners !== 'undefined') {
                return this.banners;
            }
            $.ajax({
                url: _global.url.api + 'home_banner',
                type: 'get',
                async: false,
                data: {type: type, limit: 200},
                success: function(res) {
                    _this.banners = res.data
                }
            });
            return this.banners;*/
            var collection = new Collection({
                url: _global.url.api + 'home_banner',
                container: 'banner-list',
                template: 'banner-tpl',
                page_dom: 'banner-pages',
                condition: {limit: 6, type: type},
                max_page: 5,
                render_item: function(key, item) {
                    this.el.append(template(this.template, item));
                }
            });
            collection.fetch();
        },

        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();

});